package com.yzx.lifeassistants.model.impl;

import java.util.Properties;

import org.json.JSONException;
import org.json.XML;

import android.content.Context;

import com.yzx.lifeassistants.bean.LectureInfo;
import com.yzx.lifeassistants.model.IQueryLectureInfo;
import com.yzx.lifeassistants.model.analysis.QueryLectureInfoAnalysis;
import com.yzx.lifeassistants.model.callback.IQueryLectureInfoCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 查询讲座报告信息
 * @author: yzx
 * @time: 2016-3-17 下午8:57:18
 */
public class QueryLectureInfoImpl implements IQueryLectureInfo {
	public static final String TAG = "QueryLectureInfoImpl";
	private String url;// URL
	private IQueryLectureInfoCB queryCallBack;// 回调

	public QueryLectureInfoImpl(Context context,
			IQueryLectureInfoCB queryCallBack) {
		this.queryCallBack = queryCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		url = properties.getProperty("query_lecture_info_url");
	}

	/**
	 * @Description: 查询讲座信息
	 */
	@Override
	public void queryLectureInfo(int id) {
		String url = this.url + "?id=" + id;
		HttpClientUtil.sendRequest(url, null, new IHttpCallBack() {

			@Override
			public void onFinish(String response) {
				response = response.replace("<![CDATA[", "").replace("]]>", "")
						.replace("<![CDATA[", "").replace("]]>", "");
				LogcatUtils.i(TAG, response);
				try {
					LogcatUtils.i(XML.toJSONObject(response).toString());
					if (!QueryLectureInfoAnalysis.isSuccess(response)) {// 查询是否成功
						LogcatUtils.e(TAG, "查询讲座信息失败");
						queryCallBack
								.queryError(IQueryLectureInfoCB.QUERY_FAILED);
					} else {// 查询成功
						LogcatUtils.i(TAG, "查询讲座信息成功");
						LectureInfo lectureInfo = QueryLectureInfoAnalysis
								.getData(response);
						if (null == lectureInfo) {
							queryCallBack
									.queryError(IQueryLectureInfoCB.QUERY_FAILED);
						} else {
							queryCallBack.querySuccess(lectureInfo);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
					LogcatUtils.e(TAG, "查询讲座信息异常");
					if (null != e.getMessage()) {
						LogcatUtils.e(TAG, e.getMessage());
					}
					queryCallBack.queryError(IQueryLectureInfoCB.QUERY_ERROR);
				}

			}

			@Override
			public void onError(Exception e) {
				e.printStackTrace();
				LogcatUtils.e(TAG, "查询讲座信息异常");
				if (null != e.getMessage()) {
					LogcatUtils.e(TAG, e.getMessage());
				}
				queryCallBack.queryError(IQueryLectureInfoCB.QUERY_ERROR);
			}
		});
	}

}
